package com.yfbao.horizon.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class FileLocalUtils {

    private  static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");


    public static List<String> fifterLog(String file , String fifter){
        File file1 = new File(file);
        List<String> stringList = new ArrayList<>();
        if(file1.length()>20*1024*1024){
            try (BufferedReader reader = new BufferedReader(new FileReader(file1))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    if(StringUtils.isBlank(fifter) ){
                        stringList.add(line);
                        continue;
                    }
                   if(StringUtils.isNotBlank(line) &&StringUtils.isNotBlank(fifter) && line.contains(fifter)){
                       stringList.add(line);
                   }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return stringList;
        }
        stringList = cn.hutool.core.io.FileUtil.readLines(file1, "utf-8");
        if(StringUtils.isNotBlank(fifter)){
            return stringList.stream().filter(str->str.contains(fifter)).collect(Collectors.toList());
        }
        return stringList;
    }


    public static List<String> fifterLogBetweenTime(String file , Long startTime,Long endTime){
        File file1 = new File(file);
        List<String> stringList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        if(file1.length()>20*1024*1024){
            try (BufferedReader reader = new BufferedReader(new FileReader(file1))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    if(StringUtils.isBlank(line) ){
                        continue;
                    }
                    //日志格式：10.3.9.68 - [2023-09-26 10:52:43.611] "GET /healthCheck HTTP/1.1" 404 5 5
                    int i = line.indexOf("[");
                    String substring = line.substring(i+1, i + 24);
//                    DateTime parse = DateUtil.parse(substring, "yyyy-MM-dd hh:mm:ss.SSS");
                    Date parse = null;
                    try {
                        parse = sdf.parse(substring);
                    } catch (ParseException e) {
                        System.out.println("parse error");
                        e.printStackTrace();
                    }
                    long time = parse.getTime();
                    if(time>=startTime && time<=endTime){
                        stringList.add(line);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return stringList;
        }
        return stringList;
    }


    public static List<String> readFileLine(String fileName){
        List<String> list =new ArrayList<>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(fileName));
            String tenantId=null;
            String line;
            while ((line = reader.readLine()) != null) {
                list.add(line);
            }
            reader.close();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public static void main(String[] args) {
        String file ="C:\\Users\\byf78\\AppData\\Roaming\\m5\\file\\54fc9f891a79b92dd8eb975b681aeef6\\杨锐捷\\Files\\sensitiveWord_-1_20240122101000.txt";
        List<String> list = readFileLine(file);
        System.out.println(list);
        for (String line : list) {
            String[] split = line.split(":");
            if(split.length<3){
                continue;
            }
            String tenantId=split[0].trim();
            System.out.println(Long.valueOf(tenantId));
        }
    }

}
